package com.example.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/switch")
@Controller
public class SwitchController {
	/**
	 * 设置分量开关
	 * @param request
	 * @param valid
	 * @return
	 */
	@PostMapping("/setAllotSwitch")
	@ResponseBody
	public String setAllotSwitch(HttpServletRequest request,String valid) {
		//valid是前端穿过来的on/off
		ServletContext context=request.getServletContext();
		context.setAttribute("autoAllotAskerSwitch", valid);
		return "success";
	}
	/**
	 * 查询开关（查询是否是自动分配咨询师）
	 * @param request
	 * @return
	 */
	@GetMapping("getAllotSwitch")
	@ResponseBody
	public Map<String, Object> getAllotSwitch(HttpServletRequest request){
		//从上下文应用域里取开关数据
		ServletContext context=request.getServletContext();
		String isAuto=(String) context.getAttribute("autoAllotAskerSwitch");
		//第一次读的时候，给一个默认值on
		if(isAuto == null || isAuto.equals("")) {
			isAuto = "on";
		}
		//拼接表格数据
		List<Map<String, Object>> list=new ArrayList<>();
		Map<String, Object> kgmp = new HashMap<>();
		kgmp.put("isAuto", isAuto);
		list.add(kgmp);
		//返回结果
		Map<String, Object> map = new HashMap<>();
		map.put("code","0");
		map.put("msg","success");
		map.put("count",1);
		map.put("data",list);
		return map;
	}
}
